#include "kbytecode.h"

#include <cstring>

void ByteCodeStream::Init(k_uint progSize)
{
	if (this->buf)
	{
		delete[] this->buf;
		this->ptr = 0;
	}

	if (progSize)
		this->buf = new unsigned char[progSize];
}

void ByteCodeStream::CleanUp()
{
	delete[] this->buf;
	this->buf = 0;
	this->ptr = 0;
}

const unsigned char* ByteCodeStream::GetStream() const
{
	return this->buf;
}

ByteCodeStream& ByteCodeStream::operator <<(const KOpcode &op)
{
	memcpy(this->buf + this->ptr, &op, sizeof(char));
	this->ptr += sizeof(char);

	return *this;
}

ByteCodeStream& ByteCodeStream::operator <<(const k_int &iv)
{
	memcpy(this->buf + this->ptr, &iv, sizeof(k_int));
	this->ptr += sizeof(k_int);

	return *this;
}

ByteCodeStream& ByteCodeStream::operator <<(const k_uint &iv)
{
	memcpy(this->buf + this->ptr, &iv, sizeof(k_uint));
	this->ptr += sizeof(k_uint);

	return *this;
}

ByteCodeStream& ByteCodeStream::operator <<(const k_real &rv)
{
	memcpy(this->buf + this->ptr, &rv, sizeof(k_real));
	this->ptr += sizeof(k_real);

	return *this;
}
